<!DOCTYPE html>
<html lang="en">
<head>
    <title>添加工程项目</title>
</head>
<body>
<form action="add.project" method="post" enctype="multipart/form-data">
    <p>
        项目名称：<input type="text" required name="projectName" />
    </p>
    <p>
        项目类型：<input type="text" required name="projectCategory" />
    </p>
    <p>
        专家人数：<input type="number" min="0" step="1" required name="professorNum" />
    </p>
    <p>
        项目状态：<input type="text" required name="isEvaluated" />
    </p>
    <p>
        采购方式：<input type="text" required name="prochaseMode" />
    </p>
    <p>
        采购部门id:
        <select id="departmentId" name="departmentId">
        </select>
        </p>
    <p>
        预算：<input type="number"  min="0" step="0.01" required name="budget" />
    </p>
    <p>
        采购公告时间：<input type="date"  required name="announceDate" />
    </p>
    <p>
        开标时间：<input type="date" required name="openDate" />
    </p>
    <p>
        开标地点：<!--<input type="text" required name="address" />-->
        <select name="address0" id="address0" onchange="getChild(this,'address1')"></select>
        <select name="address1" id="address1" onchange="getChild(this,'address2')"></select>
        <select name="address2" id="address2" onchange="getChild(this ,'address3')"></select>
        <select name="address3" id="address3"></select>

        <input type="text" name="address4" placeholder="街道门牌号" />
    </p>
    <p>
        成交公告时间：<input type="date"  name="dealDate" />
    </p>
    <p>
        公告发布网址：<input type="text"  name="url" />
    </p>
    <p>
        成交单位：<input type="text"  name="dealCompany" />
    </p>
    <p>
        成交价格：<input type="number"  min="0" step="0.01" name="settlePrice" />
    </p>
    <p>
        项目联系人姓名：<input type="text" required name="linkMan" />
    </p>
    <p>
        项目联系人电话：<input type="text" required name="mobliePhone" />
    </p>
    <p>
        项目文件：<input type="file"  accept="*" name="projectFilePath" />
    </p>
    <p>
        创建者：<input type="text" required name="createBy" />
    </p>
    <p>
        创建时间：<input type="date"  name="createDate" />
    </p>
    <p>
        采购部门： <input  type="text"  name="department" />

    </p>
    <p>
        修改者：<input type="text"  name="updateBy" />
    </p>
    <p>
        更新时间：<input type="date" required name="updateDate" />
    </p>
    <p>
        报到时间：<input type="date"  name="registerTime" />
    </p>
    <p>
        报到地点：<input type="text"  name="registerAddress" />
    </p>
    <p>
        评标委员会组成人数：<input type="number"  min="0" step="1" name="committee" />
    </p>
    <p>
        采购代表：<input type="text"  name="professional" />
    </p>
    <p>
        完成状态：<input type="number"  min="0" max="1" step="1" name="status" />
    </p>
    <p>
        <input type="submit" value="保存"/>
        <input type="reset" value="重填"/>
    </p>
</form>
<script>

    function getChild(t,nid){
        //value是postioname, text也是
        var xhr=new XMLHttpRequest();
        let cod="";
        if(t.options.length==0)
            cod=""
        else{
            cod=t.options[t.selectedIndex].getAttribute("codeid");
        }
        if(cod==null)
            cod="";

        xhr.open("get","findAddress.project?codeid="+cod,true);//第三个参数，是否是异步
        xhr.send(null);
        xhr.onload=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let res=xhr.response;
                let objs=JSON.parse(res);
                var n=document.getElementById(nid);
                n.options.length=0;//清空后一个
                for (let i = 0; i < objs.length; i++) {
                    let o=document.createElement("option");
                    o.value=objs[i].positionName;
                    o.innerText=objs[i].positionName;
                    //o.codeid=objs[i].codeId;
                    o.setAttribute("codeid",objs[i].codeId)
                    n.appendChild(o);
                }
               // n.onchange();//!!!!
                if ("createEvent" in document) {
                    var evt = document.createEvent("HTMLEvents");
                    evt.initEvent("change", false, true);
                    n.dispatchEvent(evt);
                }
                else
                    n.fireEvent("onchange");
            }
        }
    }

    getChild(document.getElementById("address0"),"address0");

    //页面加载完成
    window.onload=function () {
        var xhr=new XMLHttpRequest();
        xhr.open("get","find.dept",true);//第三个参数，是否是异步
        xhr.send(null);
        console.log("xhr-----",xhr);
        //请求返回
        xhr.onload=function () {
            //console.log("status",xhr.status);
            if(xhr.status==200){
                let res=xhr.responseText;
                //
                let objs=JSON.parse(res);
                //console.log(objs);
                let d=document.getElementById("departmentId");
                for (let i = 0; i < objs.length; i++) {
                    //console.log(objs[i]+"<>><>>?????");
                    //d.innerHTML="<option value='xxx'>yyy</option>";
                    d.appendChild(new Option(objs[i].departmentname,objs[i].id));
                }
            }else{
                alert("请求出错");
            }

        }
    }
</script>
</body>
</html>